package com.woniu.cms.contract.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.cms.contract.core.entity.FrameworkContractManagement;
import com.woniu.cms.contract.core.mapper.FrameworkContractManagementMapper;
import com.woniu.cms.contract.core.service.IFrameworkContractManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.api.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@Service
public class FrameworkContractManagementServiceImpl extends ServiceImpl<FrameworkContractManagementMapper, FrameworkContractManagement> implements IFrameworkContractManagementService {


    @Autowired
    private FrameworkContractManagementMapper frameworkContractManagementMapper;

    /*
    * 编辑查询
    * */
    @Override
    public Result queryFramMana(String frameworkContractCode) {
        FrameworkContractManagement queryFamOne = frameworkContractManagementMapper.queryFramMana(frameworkContractCode);

        return Result.common(queryFamOne);
    }

    /*
    * 创建或者修改
    * */
    @Override
    public Result addAuditFrameManage(FrameworkContractManagement frameworkContractManagement) {

        QueryWrapper<FrameworkContractManagement> wrapper = new QueryWrapper<>();


        wrapper.eq("framework_contract_code",frameworkContractManagement.getFrameworkContractCode());

        Result result = this.queryFramMana(frameworkContractManagement.getFrameworkContractCode());
        System.out.println("management = " + result.getData());
        if (StringUtils.isEmpty(result.getData())){

            boolean b = this.save(frameworkContractManagement);
            return b == true ? Result.ok() : Result.error();
        }
        boolean b = this.update(frameworkContractManagement, wrapper);

        return b == true ? Result.ok() : Result.error();

    }

    /*
    * 分页和模糊查询
    * */
    @Override
    public Result queryFrameManage(String frameworkContractCode, String clientName, Integer currentPage, Integer pageSize) {
        Page<FrameworkContractManagement> page = new Page<>(currentPage,pageSize);

        QueryWrapper<FrameworkContractManagement> wrapper = new QueryWrapper<>();

        if (frameworkContractCode != null && frameworkContractCode != ""){
            wrapper.like("framework_contract_code",frameworkContractCode);
        }
        if (frameworkContractCode != null && frameworkContractCode != ""){
            wrapper.like("client_name",clientName);
        }
        Page<FrameworkContractManagement> managementPage = this.page(page, wrapper);
        List<FrameworkContractManagement> records = managementPage.getRecords();
        long total = managementPage.getTotal();

        HashMap<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);

        return Result.common(map);
    }
}
